// pages/customerNeedSubmit/index.js
const app = getApp();
const network = require("../../utils/network.js");
const util = require("../../utils/util.js");

import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listParams:{
      page: 1,
      page_size: 10,
      pages: 1
    },
    listData: []
  },

  onShow(){
    this.getList();
  },

  getList(first){
    util.showLoading('加载中');
    network.GET({
      uri: app.api.customerNeedList,
      params: this.data.listParams,
      success: (res) => {
        console.log(res)
        let list = [];
        const rs = (res.data.records || []).map(d => {
          d.collapse_actives = [];
          d.buildings_str = d.buildings.map(d => d.name).join('\n');
          return d;
        })
        if(!!first){
          list = rs;
        }else{
          list = this.data.listData.concat(rs);
        }
        this.setData({
          listData: list,
          'listParams.pages': res.data.pages
        })
      },
      complete: (res) => {
        util.hideLoading()
      }
    })
  },

  onClose(event) {
    const { position, instance } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
      case 'right':
        instance.close();
        break;
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      'listParams.page': 1,
      'listParams.pages': 1
    },function(){
      this.getList(true);
    })
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.listParams.pages > this.data.listParams.page){
      this.setData({
        'listParams.page': this.data.listParams.page+1
      },function(){
        this.getList(false);
      })
    }
  },

  changeItemCollapseActives(event){
    console.log(event);
    const key = 'listData['+event.currentTarget.dataset.index+'].collapse_actives'
    this.setData({
      [key]: event.detail
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})